package com.gmu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gmu.domain.ResResult;
import com.gmu.domain.vo.PageVo;
import com.gmu.domain.vo.TblSampleVo;
import com.gmu.mapper.TblOrderMapper;
import com.gmu.mapper.TblUserMapper;
import com.gmu.pojo.TblActivities;
import com.gmu.pojo.TblOrder;
import com.gmu.pojo.TblSample;
import com.gmu.mapper.TblSampleMapper;
import com.gmu.pojo.TblUser;
import com.gmu.service.TblSampleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 采样记录表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-07-24
 */
@Service
public class TblSampleServiceImpl extends ServiceImpl<TblSampleMapper, TblSample> implements TblSampleService {

    @Autowired
    private TblSampleMapper tblSampleMapper;

    @Autowired
    private TblOrderMapper tblOrderMapper;

    @Autowired
    private TblUserMapper tblUserMapper;

    /**
     * 提交采样记录(第一次扫码)
     * @param unitsId 单位id
     * @param userId 用户id
     * @param idNumber 身份证号
     * @param activitiesId 活动id
     * @return
     */
    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED, rollbackFor = Exception.class)//事务：读取已提交数据(会出现不可重复读和幻读)
    public ResResult<?> commitSampleRecord(String unitsId, String userId, String idNumber, String activitiesId) {
        TblUser tblUser = tblUserMapper.getUserByIdNumber(idNumber);
        if (Objects.isNull(tblUser)){
            return new ResResult<>(404,"身份证号错误");
        }
        Integer activitiesIdInt = Integer.parseInt(activitiesId);
        TblOrder tblOrder = tblOrderMapper.searchOrderByUserIdAndActivitiesId(tblUser.getUserId(), activitiesIdInt , idNumber);
        if (Objects.isNull(tblOrder)){
            return new ResResult<>(404,"您并未预约");
        }
        Integer count = tblSampleMapper.getSampleByIdNumberAndActivitiesId(idNumber,activitiesIdInt,0);//已进行第一次扫码
        if (count > 0){
            return new ResResult<>(500,"不可重复提交");
        }
        Integer unitsIdInt = Integer.parseInt(unitsId);
        TblSample tblSample = new TblSample();
        tblSample.setUnitsId(unitsIdInt);
        tblSample.setUserId(unitsIdInt);
        tblSample.setIdNumber(idNumber);
        tblSample.setActivitiesId(activitiesIdInt);
        tblSample.setSampleTime(new Date());
        tblSample.setStatus(0);//扫码之后为已扫描状态
        if ((tblSampleMapper.insert(tblSample) < 1)){
            return new ResResult<>(500,"提交失败");
        }
        return new ResResult<>(200,"提交成功");
    }

    /**
     * 提交检测时间(第二次扫码)
     * @param idNumber 身份证号
     * @param activitiesId 活动id
     * @return
     */
    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED, rollbackFor = Exception.class)//事务：读取已提交数据(会出现不可重复读和幻读)
    public ResResult<?> commitTestTime(String idNumber, String activitiesId) {
        Integer activitiesIdInt = Integer.parseInt(activitiesId);
        Integer count = tblSampleMapper.getSampleByIdNumberAndActivitiesId(idNumber,activitiesIdInt,1);//已进行第二次扫码
        if (count > 0){
            return new ResResult<>(500,"不可重复提交");
        }
        if ((tblOrderMapper.setOrderToTested(idNumber,activitiesIdInt)) < 1){
            return new ResResult<>(500,"您并未预约");
        }
        if ((tblSampleMapper.commitTestTime(idNumber,activitiesIdInt,new Date(),1) < 1)){
            return new ResResult<>(500,"你并被录入,请前往录入");
        }
        return new ResResult<>(200,"提交成功");
    }

    @Override
    public ResResult<?> getUserSampleByUserId(Integer userId) {

        List<TblSample> tblSamples = tblSampleMapper.getUserSampleByUserId(userId);

        return new ResResult<>(200,tblSamples);
    }

    @Override
    public PageVo<?> getSampleRecordList(Integer pageNum, Integer pageSize, TblSampleVo tblSample, String beginTime, String endTime) {
        Page<TblActivities> page = new Page<>(pageNum,pageSize);
        IPage<TblActivities> iPage = tblSampleMapper.getSampleRecordList(page,tblSample,beginTime,endTime);
        PageVo<TblActivities> pageVo = new PageVo<>();
        pageVo.setCurrent((int) iPage.getCurrent());
        pageVo.setSize((int) iPage.getSize());
        pageVo.setList(iPage.getRecords());
        pageVo.setTotal(iPage.getTotal());
        pageVo.setCode(200);
        return pageVo;
    }

    @Override
    public ResResult<?> deleteSampleRecord(Integer[] sampleRecordIds) {
        int result = tblSampleMapper.deleteSampleRecord(sampleRecordIds);
        return result > 0 ? new ResResult<>(200,"删除成功"):new ResResult<>(200,"删除失败");
    }

    @Override
    public List<TblSampleVo> getSampleVoList( TblSampleVo tblSample, String beginTime, String endTime) {
        List<TblSampleVo> sampleVoList = tblSampleMapper.getSampleVoList(tblSample,beginTime,endTime);
        for (TblSampleVo tblSampleVo:sampleVoList) {
            if (tblSampleVo.getStatus()==0) {
                tblSampleVo.setSta("已扫码");
            } else {
                tblSampleVo.setSta("已检测");
            }
        }
        return sampleVoList;
    }

    @Override
    public ResResult<?> getStudentByActivitiesId(Integer activitiesId) {

        List<TblSample> tblSample = tblSampleMapper.getStudentByActivitiesId(activitiesId);

        return new ResResult<>(200,tblSample);
    }

    @Override
    public ResResult<?> getTeacherByActivitiesId(Integer activitiesId) {

        List<TblSample> tblSample = tblSampleMapper.getTeacherByActivitiesId(activitiesId);

        return new ResResult<>(200,tblSample);
    }

    @Override
    public ResResult<?> getSampleRecordByArea(Integer activitiesId, Integer unitsId, Integer status) {

        List<TblSample> tblSamples = tblSampleMapper.getSampleRecordByArea(activitiesId,unitsId,status);

        return new ResResult<>(200,tblSamples);

    }


}
